
********************************************************************************
*		CREATE TIME TO SHOOTING & CLEAN SHOOTING CHARACTERISTICS FOR ANALYSIS 
********************************************************************************
** Clean shootings data
use ../raw/shootings/LAshootings_cleaned.dta,clear
egen incident_id = group(incident_address sht_date)
drop if geoid_block==.
gen cb = "0" + block
assert length(cb)==15
keep if sht_date>date("07nov2000","DMY") // 2000 general election date
keep if sht_date<=date("08nov2016","DMY")  // 2016 general election date
gen weap_unknown = weapon_flag==0
gen attack_unknown = weapon_flag==0
gen attack_yes = attack==1
gen attack_no = attack==0 & attack!=.
foreach v in attack gun knife none {
	replace `v' = 0 if `v'==.
}
assert attack_yes + attack_no + attack_unknown==1
assert gun + knife + none + weap_unknown==1
gen resident_yes = resident==1
gen resident_no = resident==0
gen resident_unknown = resident==.
rename sht_other sht_asian 	
#delimit ;
global sht_char "sht_id incident_id sht_date sht_white sht_black sht_hispanic sht_asian 
	ttl_mentions any_mentions attack_yes attack_no attack_unknown 
	gun knife none weapon_flag weap_unknown 
	resident resident_yes resident_no resident_unknown";
#delimit cr
duplicates report sht_date cb name
sort sht_date cb name
save ../temp/shootings.dta,replace

** Merge election dates to vc_census file, add: current_elec_date prev_elec_date
use "../raw/elections/la election dates.dta",clear
keep if substr(election,1,2)=="GG" | substr(election,1,2)=="PG"
gen curr_elec_date = date(election_date1, "MDY")
gen prev_elec_date = curr_elec_date[_n -1]
format %td curr_elec_date prev_elec_date
gen elec_yr = year(curr_elec_date)
keep elec_yr *_elec_date
gen btwn_elec_days = curr_elec_date - prev_elec_date
keep if elec_yr>2000
drop prev_elec_date
ren curr_elec_date elec_date
save ../temp/elec_dates.dta,replace
use cb elec_yr using ../data_intermediate/reg_2002_2016.dta,clear
merge m:1 elec_yr using ../temp/elec_dates.dta,assert(1 3) keep(3) nogen
save ../temp/cb_elecdates.dta,replace

** Merge shootings CBs with shooting
use ../temp/shootings.dta,clear
count
global shoot_count = r(N)
global cnt = 0
forval i=1/$shoot_count {
	qui use ../temp/shootings.dta in `i',clear
	qui merge 1:m cb using  ../temp/cb_elecdates.dta, assert(1 2 3) keep(3) nogen
	qui gen days_to_elec =  sht_date - elec_date
	qui keep if days_to_elec > -btwn_elec_days & days_to_elec<=0
	qui gen cb_shoot = 1
	qui count
	if r(N)!=0 {
		global cnt = $cnt +1
		save ../temp/sht${cnt}.dta,replace
	}
}
use ../temp/sht1.dta, clear
qui rm ../temp/sht1.dta
forval i=2/$cnt {
	append using ../temp/sht`i'.dta
	rm ../temp/sht`i'.dta
}
save ../data_intermediate/merged_shootings_all.dta,replace

** Save police killings data for summary stats in Table 1
use  ../data_intermediate/merged_shootings_all.dta,clear
merge m:1 cb elec_yr using ../data_intermediate/reg_2002_2016.dta, ///
	assert(2 3)  keepusing( all_c00 all_c10 )
gen sample_main =  all_c00>=5 &  all_c10>=5 
keep if _merge==3
keep if elec_yr>=2004 & elec_yr<=2010
count
count if sample_main==1
ren weap_unknown weap_miss
ren gun weap_gun
ren knife weap_knife
ren none weap_unarmed
assert weap_gun + weap_knife + weap_unarmed + weap_miss==1
foreach v in weap_unarmed weap_knife weap_gun weap_miss {
	tab `v',m
}
keep if sample_main==1
count
keep sht_id cb incident_id sht_date sht_black sht_hispanic sht_white sht_asian sht_male ///
	age any_mentions ttl_mentions weap_unarmed weap_knife weap_gun weap_miss sample_main
save ../data/police_killings.dta,replace

// collapse to cb per election level, merge back shoot vars later
use  ../data_intermediate/merged_shootings_all.dta,clear
keep if elec_yr>=2004 // keep only shootings that occur between 2002 and 2010 elections
preserve 
gen cb_sht_count = cb_shoot
gen sht_date_max = sht_date
gen sht_date_min = sht_date
collapse (max) $sht_char sht_date_max (min) sht_date_min (sum) cb_sht_count, by(cb elec_yr)
// we take max of sht_char across all shootings within a given cb-election (UPDATE)
save ../temp/shoot_cnt_vars_by_cb_elec.dta,replace
restore 
drop $sht_char
bys cb elec_yr: keep if _n==1
merge 1:1 cb elec_yr using ../temp/shoot_cnt_vars_by_cb_elec.dta,assert(3) nogen
// all shoot char are indicators, so set to 0 for missing
foreach v in $sht_char {
	replace `v' = 0 if `v'==.
}
keep cb elec_yr $sht_char sht_date_max sht_date_min cb_shoot cb_sht_count
save ../temp/merged_shootings_collapsed.dta,replace

// merge to census data at cb-elec_yr level
use cb elec_yr all_c00 all_c10 using ../data_intermediate/reg_2002_2016.dta,clear
merge 1:1 cb elec_yr using ../temp/merged_shootings_collapsed.dta, assert(1 3) nogen
// mark elec_yr after first shooting from 2000-2010
gen yr0i = 3000
replace yr0i = elec_yr if cb_shoot==1
bys cb (elec_yr): egen yr0 = min(yr0i)
replace yr0 = . if yr0==3000
replace cb_shoot = 0 if cb_shoot==.
replace cb_sht_count = 0 if cb_sht_count==.
// Isolate max of shooting char across the first shooting/set of shootings
//		below also sets shooting char to max for all obs for a given CB
foreach v in $sht_char {
	gen `v'_yr0 = 0
	replace `v'_yr0 = `v' if elec_yr == yr0 & yr0!=.
	bys cb (elec_yr): egen max_`v'_yr0 = max(`v'_yr0)
	ren `v' `v'_old
	ren max_`v'_yr0 `v'
	drop `v'_old
}
replace sht_date = . if sht_date==0
format sht_date sht_date_min sht_date_max %td
keep cb elec_yr cb_shoot cb_sht_count $sht_char sht_date_min sht_date_max yr0 ///
	all_c00 all_c10 
gen sample_main = all_c00>=5 & all_c10>=5
save ../temp/shootings_cb_by_elec.dta,replace
save ../data_intermediate/shootings_cb_by_elec.dta,replace

// make tau variable for 2002-2010 analysis
use ../temp/shootings_cb_by_elec.dta,clear
keep if elec_yr<=2010
replace yr0 =. if yr0>2010
cap drop tau
gen tau = (elec_yr - yr0)/2 if yr0!=.
replace tau = . if yr0==2002
gen treat = tau!=.
tab tau elec_yr,m
tab tau elec_yr if sample_main==1,m
gen treat_post = tau>=0 & tau<.
save ../temp/shootings_tau.dta,replace

*** MAKE VARIABLES FOR MAIN ANALYSIS
use ../temp/shootings_tau.dta,clear
	foreach v in $sht_char {
		replace `v' = 0 if yr0==. 
	}
	//HETEROGENEITY BY WEAPON TYPE
	foreach v in gun knife none {
		replace `v' = 0 if `v'==.
	}
	// adjust for max shoot char across all shootings in given cb-election
	gen weap_cnt = none + knife + gun
	replace knife = 0 if gun==1 // if gun present update to gun so mutually exclusive vars
	assert none==0 if knife==1 | gun ==1
	gen armed = knife | gun
	replace weap_unknown=0 if weap_cnt>0
	assert weap_unknown + none + knife + gun ==1 if cb_shoot==1 & yr0>2002
	drop weap_cnt
	gen sample_armed_known = weap_unknown==0 & none==0 & sample_main==1
	gen sample_unarmed_known = weap_unknown==0 & armed==0 & sample_main==1
	
	//HETEROGENEITY BY WHETHER VICTIM IS RESIDENT OF NEIGHBORHOOD
	gen tp_resident = treat_post*(resident_yes==1)
	gen tp_not_resident = treat_post*(resident_no==1)
	gen tp_unknown_resident = treat_post*(resident_unknown==1)
	assert treat_post == tp_resident + tp_not_resident + tp_unknown_resident

	//HETEROGENEITY BY VICTIM RACE
	foreach r in black hispanic white asian {
		gen tp_`r' = treat_post * (sht_`r'==1)
	}
	gen tp_blackhispanic = tp_black | tp_hispanic
	gen tp_whiteasian = tp_white | tp_asian
	assert treat_post == tp_blackhispanic + tp_whiteasian 
	
	// MULITPLE SHOOTINGS DURING STUDY PERIOD
	// count all shootings during study period
	bys cb (elec_yr): egen shootings_cnt = sum(cb_sht_count)
	replace shootings_cnt = 0 if treat==0
	// multiple shootings that occur between multiple election cycles
	bys cb (elec_yr): egen multi_treat = sum(cb_shoot)
	replace multi_treat = multi_treat - 1
	replace multi_treat = 0 if treat==0
	tab multi_treat if sample_main==1,m
	ren cb_shoot shooting
	keep cb elec_yr shooting tau treat treat_post tp_not_resident tp_resident ///
		tp_blackhispanic tp_whiteasian shootings_cnt multi_treat ///
		sample_main sample_armed_known sample_unarmed_known ///
		weap_unknown none knife gun armed resident_* cb_sht_count
save ../data_intermediate/main_shootings.dta,replace
